package org.zjt.mock.demo.mock.controller;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.context.request.async.DeferredResult;
import org.zjt.mock.demo.mock.annotation.RequestParam2;
import org.zjt.mock.demo.mock.domain.User;
import org.zjt.mock.demo.mock.service.RemoteUserService;
import org.zjt.mock.demo.mock.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

/**
 * @author juntao.zhang
 * @version V1.0
 * @Description: TODO
 * @Package or.zjt.mock.demo.mock.controller
 * @date 2018/6/3 16:23
 */
@RestController
@Validated
public class HelloController {

    @Autowired
    private UserService userService ;

//    @Autowired
//    private RemoteUserService remoteUserService;

    @Autowired
    private MessageSource messageSource;


    @GetMapping("/i18n")
    public String i18n() {
        Locale locale = LocaleContextHolder.getLocale();
        String name = messageSource.getMessage("name", new Object[]{"ssssssssss"}, locale);
        String no = messageSource.getMessage("no", null, locale);
        return name  + " <br>  " + no;
    }


    @GetMapping("/")
    public String  index() {
        return "Hello World!";
    }

    /**
     * RequestParam 常用来处理简单类型的绑定，
     * @param user
     * @return
     */
    @PostMapping("/user")
    public String  save(@RequestParam String name,@RequestParam Integer age) {
        userService.saveUser(User.builder().age(age).name(name).build());
        return "ok";
    }


    /**
     * ModelAttribute 可以省略
     * @param user
     * @return
     */
    @PostMapping("/user3")
    @Valid
    public String save(@ModelAttribute User user) {
        userService.saveUser(user);
        return "ok";
    }


    @PostMapping("/user2")
    public String  save2(@NotNull @RequestBody User user) throws BindException {
        userService.saveUser(user);
        return "ok";
    }


    @GetMapping("/user")
    public List<User> all() {
        return userService.findAllUser();
    }


    @GetMapping("/user-call")
    public Callable<List<User>> allCallable() {
        return () -> userService.findAllUser();
    }

    @GetMapping("/user-defer")
    public DeferredResult<List<User>> allDefer() {
        DeferredResult<List<User>> deferredResult = new DeferredResult<>();
        deferredResult.setResult(userService.findAllUser());
        return deferredResult ;
    }


    @GetMapping("/local-date-time")
    public DeferredResult<LocalDateTime> parseLocalDateTime(@RequestParam("localDateTime") LocalDateTime localDateTime) {
        DeferredResult<LocalDateTime> deferredResult = new DeferredResult<>();
        deferredResult.setResult(localDateTime);
        return deferredResult ;
    }


    @GetMapping("/local-date-time-2")
    public DeferredResult<LocalDateTime> parseLocalDateTime2(@RequestParam2("localDateTime") LocalDateTime localDateTime) {
        DeferredResult<LocalDateTime> deferredResult = new DeferredResult<>();
        deferredResult.setResult(localDateTime);
        return deferredResult ;
    }


    @GetMapping("/user/{name}")
    public List<User> findUser(@PathVariable String name) {
        return userService.findAllUser().stream().filter(item -> Objects.equals(item.getName(),name)).collect(Collectors.toList());
    }



//    @GetMapping("/user1/{age}")
//    public List<User> findUser(@PathVariable Integer age) {
//        return Collections.unmodifiableList(remoteUserService.findUsers(age));
//    }
}
